package com.algorithm.hint;

import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;

import java.util.*;

public class HintShardingAlgorithm implements org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm<String> {
    /**
     * 自定义分片规则 对2取余分表
     *
     * @param availableTargetNames
     * @param shardingValue
     * @return
     */
    @Override
    public Collection<String> doSharding(Collection<String> availableTargetNames,
                                         HintShardingValue<String> shardingValue) {
        Collection<String> result = new ArrayList<>();
        availableTargetNames.parallelStream().forEach(o->{
            for (String value : shardingValue.getValues()) {
                //表尾号==尾号2的余数
                if (o.endsWith(value)) {
                    result.add(o);
                }
            }
        });

        return result;
    }
}
